package com.example.nbshangpping;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.nbshangpping.History.HistoryActivity;
import com.example.nbshangpping.buy.CartGoodsActivity;
import com.example.nbshangpping.login.LoginRegActivity;
import com.example.nbshangpping.login.PersonCenterActivity;
import com.example.nbshangpping.login.UserBean;
import com.example.nbshangpping.utils.SaveUserUtils;
import com.example.nbshangpping.utils.URLUtils;

import java.io.File;

/**
 *我的碎片
 */
public class MeFragment extends Fragment {
    Button cartBtn,historyBtn,centerBtn,aboutBtn,settingBtn;
    ImageView headIv;
    TextView nicknameTv;
    String imgpath;
    private UserBean.DataBean userInfo;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.fragment_me, container, false);
        userInfo = SaveUserUtils.getUserInfo(getContext());
        initView(view);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        setHeadview();
    }

    public void setHeadview() {
        userInfo = SaveUserUtils.getUserInfo(getContext());
        if (userInfo!=null) {
            imgpath = URLUtils.getImgPath(getContext(),userInfo.getPhone());
            File file = new File(imgpath);
            if (file.exists()) {
                Bitmap bitmap = BitmapFactory.decodeFile(imgpath);
                headIv.setImageBitmap(bitmap);
            }else{
                headIv.setImageResource(R.mipmap.icon);
            }
        }else{
            headIv.setImageResource(R.mipmap.icon);
        }
        if (userInfo!=null) {
            String nickname = userInfo.getNickname();
            nicknameTv.setText(nickname);
        }else{
            nicknameTv.setText("请先登录");
        }
    }

    private void initView(View v) {
        cartBtn = v.findViewById(R.id.frag_me_btn_cart);
        historyBtn = v.findViewById(R.id.frag_me_btn_history);
        centerBtn = v.findViewById(R.id.frag_me_btn_center);
        aboutBtn = v.findViewById(R.id.frag_me_btn_about);
        settingBtn = v.findViewById(R.id.frag_me_btn_setting);
        headIv = v.findViewById(R.id.frag_me_iv);
        nicknameTv = v.findViewById(R.id.frag_me_tv_name);

        cartBtn.setOnClickListener(listener);
        historyBtn.setOnClickListener(listener);
        centerBtn.setOnClickListener(listener);
        aboutBtn.setOnClickListener(listener);
        settingBtn.setOnClickListener(listener);
        headIv.setOnClickListener(listener);
    }

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.frag_me_btn_cart:
                    islogin2(0);
                    break;
                case R.id.frag_me_btn_history:
                    islogin1(0);
                    break;
                case R.id.frag_me_btn_center:
                    islogin(0);
                    break;
                case R.id.frag_me_btn_about:
                    Intent intent=new Intent(getContext(),AboutActivity.class);
                    startActivity(intent);
                    break;
                case R.id.frag_me_btn_setting:
                    Intent intent1=new Intent(getContext(),SetActivity.class);
                    startActivity(intent1);
                    break;
                case R.id.frag_me_iv:
                    break;
            }
        }
    };
    private void islogin2(int flag){
        UserBean.DataBean userinfo = SaveUserUtils.getUserInfo(getContext());
        if (userinfo==null){
            Intent it1=new Intent(getContext(),LoginRegActivity.class);
            it1.putExtra("flag",flag);
            startActivity(it1);
        }else {
            Intent it2 = new Intent(getContext(), CartGoodsActivity.class);
            startActivity(it2);
        }
    }

    private void islogin1(int flag1) {
        UserBean.DataBean userinfo = SaveUserUtils.getUserInfo(getContext());
        if (userinfo==null){
            Intent it1=new Intent(getContext(), LoginRegActivity.class);
            it1.putExtra("flag1",flag1);
            startActivity(it1);
        }else {
            Intent intent = new Intent(getContext(), HistoryActivity.class);
            startActivity(intent);
        }
    }

    private void islogin(int flag){
        UserBean.DataBean userinfo = SaveUserUtils.getUserInfo(getContext());
        if (userinfo==null){
            Intent it1=new Intent(getContext(), LoginRegActivity.class);
            it1.putExtra("flag",flag);
            startActivity(it1);
        }else {
            Intent intent = new Intent(getContext(), PersonCenterActivity.class);
            startActivity(intent);
        }
    }
}